package org.tangerinejunction.imstats.stats;

import java.util.ArrayList;
import java.util.Calendar;

import org.tangerinejunction.imstats.data.Account;
import org.tangerinejunction.imstats.logs.LogFile;
import org.tangerinejunction.imstats.util.TimeRange;

/**
 * AccountStatistics is a library of functions for calculating
 * various statistics on accounts, usually over a particular time range.
 * 
 * @author meganthorsen
 */
public class AccountStatistics 
{
	/**
	 * 
	 * @param account	the Account to analyze
	 * @param range		the TimeRange over which to analyze the Account
	 * @return			the total file size logged for the Account over the TimeRange
	 */
	public static long getFileSize(Account account, TimeRange range)
	{
		ArrayList<LogFile> logFiles = account.getLogFiles();
		long totalSize = 0l;
		for (LogFile logFile : logFiles)
		{
			if (range.contains(logFile.getDate()))
				totalSize += FileStatistics.getFileSize(logFile);
		}
		return totalSize;
	}
	
	/**
	 * 
	 * @param account	the Account to analyze
	 * @param range		the TimeRange over which to analyze the Account
	 * @return			the total number of files logged for this Account in the TimeRange
	 */
	public static int getFileCount(Account account, TimeRange range)
	{
		ArrayList<LogFile> logFiles = account.getLogFiles();
		int fileCount = 0;
		for (LogFile logFile : logFiles)
			if (range.contains(logFile.getDate()))
				fileCount++;
		return fileCount;
				
	}
	
	/**
	 * 
	 * @param account	the Account to analyze
	 * @param range		the TimeRange over which to analyze the Account
	 * @return			the first log date for the Account over the TimeRange
	 */
	public static Calendar getFirstLogDate(Account account, TimeRange range)
	{
		ArrayList<LogFile> logFiles = account.getLogFiles();
		Calendar firstDate = range.getEnd();
		for (LogFile logFile : logFiles)
		{
			if (logFile.getDate().before(firstDate))
				firstDate = logFile.getDate();
		}
		return firstDate;
	}
	
	/**
	 * 
	 * @param account	the Account to analyze
	 * @param range		the TimeRange over which to analyze the Account
	 * @return			the last log date for the Account over the TimeRange
	 */
	public static Calendar getLastLogDate(Account account, TimeRange range)
	{
		ArrayList<LogFile> logFiles = account.getLogFiles();
		Calendar endDate = range.getBeginning();
		for (LogFile logFile : logFiles)
		{
			if (logFile.getDate().after(endDate))
				endDate = logFile.getDate();
		}
		return endDate;
	}
}
